package com.liyiqing.secondhandmall.productservice.controller;

import java.util.*;

import com.liyiqing.commonservice.utils.NowTime;
import com.liyiqing.commonservice.utils.StatusCode;
import com.liyiqing.commonservice.validator.methodgroup.AddGroup;
import com.liyiqing.commonservice.validator.methodgroup.DeleteGroup;
import com.liyiqing.commonservice.validator.methodgroup.UpdateGroup;
import com.liyiqing.commonservice.vo.ProductDetailVo;
import com.liyiqing.commonservice.vo.ProductShowVo;
import com.liyiqing.secondhandmall.productservice.entity.ProductSpuImagesEntity;
import com.liyiqing.secondhandmall.productservice.service.ProductSpuImagesService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.liyiqing.secondhandmall.productservice.entity.ProductSpuInfoEntity;
import com.liyiqing.secondhandmall.productservice.service.ProductSpuInfoService;
import com.liyiqing.commonservice.utils.R;

import javax.annotation.Resource;


/**
 * spu商品信息
 *
 * @author LiYiQing
 * @email 1570865668@qq.com
 * @date 2022-02-18 23:36:20
 */
@RestController
@RequestMapping("productService/productSpuInfo")
public class ProductSpuInfoController {

    @Autowired
    private ProductSpuInfoService productSpuInfoService;

    @Autowired
    private ProductSpuImagesService productSpuImagesService;

    //redis工具
    @Resource
    private StringRedisTemplate stringRedisTemplate;



    /**
     * 新增商品,并同时存储对应的图片
     */
    @RequestMapping("/insertProduct")
    public R insertProduct(@Validated({AddGroup.class}) @RequestBody ProductDetailVo productDetailVo){

        //创建返给前端的对象
        ProductSpuInfoEntity productSpuInfo = new ProductSpuInfoEntity();
        //将前端传来的属性值赋给数据库实体一份
        BeanUtils.copyProperties(productDetailVo,productSpuInfo);

        //1.首先向商品表中插入信息
        productSpuInfo.setPublishStatus(1);//设置为上架状态
        productSpuInfo.setSaleCount(0L);//设置销售量为0
        String time = NowTime.getNowTime();
        productSpuInfo.setCreateTime(time);//设置创建时间
        productSpuInfo.setUpdateTime(time);//设置更新时间

        //将商品信息保存至数据库
        productSpuInfoService.save(productSpuInfo);

        //2.获取前端传来的图片地址
        List<String> images = productDetailVo.getImagesUrl();
        //3.获取商品主键，将此商品的图片保存至数据库
        Long id = productSpuInfo.getId();

        if (images != null && images.size()>0) {
            for (int i = 0; i < images.size(); i++) {
                ProductSpuImagesEntity productSpuImage = new ProductSpuImagesEntity();
                productSpuImage.setSpuId(id);//设置图片的商品id
                productSpuImage.setStatus(1);//设置图片可用
                productSpuImage.setImgUrl(images.get(i));//设置图片地址
                productSpuImage.setImgSort(i);//设置图片顺序
                //向数据库保存图片
                productSpuImagesService.save(productSpuImage);

            }
        }
        productDetailVo.setId(id);
        productDetailVo.setSaleCount(0L);
        productDetailVo.setPublishStatus(1);
        return  R.ok().put("data",productDetailVo);
    }



    /**
     * TODO 修改商品信息 -未完成
     * 修改商品信息，包括对应的图片表
     */
    @RequestMapping("/update")
    public R update(@Validated({UpdateGroup.class})@RequestBody ProductSpuInfoEntity productSpuInfo){
//        productSpuInfoService.updateById(productSpuInfo);

        return R.ok();
    }




    /**
     * 根据商品主键id，获取一个商品的详细信息
     */
    @RequestMapping("/selectProductById/{id}")
    public R selectProductById(@PathVariable("id") Long id){
        ProductDetailVo productDetailVo = productSpuInfoService.selectProductDetailVoById(id);
        if (productDetailVo == null){
            return R.error(StatusCode.DATA_NOT_FOUND.getCode(),StatusCode.DATA_NOT_FOUND.getMsg());
        }
        return R.ok().put("data", productDetailVo);
    }


    /**
     * 根据商品主键id，获取一个商品的简要信息
     */
    @RequestMapping("/selectShowProductById/{id}")
    public R selectShowProductById(@PathVariable("id") Long id){
        ProductShowVo productShowVo = productSpuInfoService.selectProductShowVoById(id);
        if (productShowVo == null){
            return R.error(StatusCode.DATA_NOT_FOUND.getCode(),StatusCode.DATA_NOT_FOUND.getMsg());
        }
        return R.ok().put("data", productShowVo);
    }


    /**
     * 根据账号id，获取该账号发布的所有商品的简要信息
     */
    @RequestMapping("/selectProductByMemberId/{id}")
    public R selectProductByMemberId(@PathVariable("id") Long id){
        return productSpuInfoService.selectProductByMemberId(id);
    }

    /**
     * 根据二级分类id，获取该所有商品的简要信息
     */
    @RequestMapping("/selectProductByGroupId/{id}")
    public R selectProductByGroupId(@PathVariable("id") Long id){
        return productSpuInfoService.selectProductByGroupId(id);
    }


    /**
     * 根据搜索框的关键字，模糊查询商品
     */
    @RequestMapping("/selectProductLikeKeyword/{keyword}")
    public R selectProductLikeKeyword(@PathVariable("keyword") String keyword){
        return productSpuInfoService.selectProductLikeKeyword(keyword);
    }



    /**
     * 查询所有商品的简要信息，在商城中显示
     */
    @RequestMapping("/selectAllProduct")
    public R selectAllProduct(){
        return productSpuInfoService.selectAllProduct();
    }




    /**
     * 根据商品id删除商品以及其相应的所有照片
     */
    @RequestMapping("/deleteProductById/{id}")
    public R deleteProductById(@Validated({DeleteGroup.class}) @PathVariable Long id){
        //1.删除商品信息
        productSpuInfoService.removeById(id);

        //2.根据商品id获取其所有的照片id
        List<Long> imagesId = productSpuImagesService.selectImagesIdByProductId(id);
        //3.根据照片id删除所有图片
        productSpuImagesService.removeByIds(imagesId);
        return R.ok();
    }



    /**
     * 根据商品id批量删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
        //1.删除商品信息
		productSpuInfoService.removeByIds(Arrays.asList(ids));

		for (int i = 0 ;i<ids.length;i++){
            //2.根据商品id获取其所有的照片id
            List<Long> imagesId = productSpuImagesService.selectImagesIdByProductId(ids[i]);
            //3.根据照片id删除所有图片
            productSpuImagesService.removeByIds(imagesId);
        }

        return R.ok();
    }

}
